import { defineStore } from "pinia";
import { useRouter } from "vue-router";

export const useAuthStore = defineStore("auth", {
    state: () => ({
        token: localStorage.getItem("token") || null,
        user: null,
    }),
    getters: {
        isAuthenticated: (state) => !!state.token,
    },
    actions: {
        setToken(token) {
            this.token = token;
            localStorage.setItem("token", token);
        },
        logout() {
            this.token = null;
            this.user = null;
            localStorage.removeItem("token");
            useRouter().push("/login");
        },
    },
});
